using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Data;using THSERP.BusinessEntities;using THSERP.BaseProvider;
using System.Windows.Forms;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraTreeList.Columns;
using THSLib;using THSERP.BaseProvider;

namespace THSERP.Modules.Department
{
    #region DepartmentModule
    //-----------------------------------------------------------
    //Generated By: THS Studio
    //Class:DepartmentModule
    //Created Date:Monday, March 01, 2010
    //-----------------------------------------------------------

    public class DepartmentModule : BaseModuleERP
    {
        #region Declare Constant
        public const String cstICDepartmentsObjectName = "ICDepartments";
        public const String cstICProductGroupsObjectName = "ICProductGroups";
        public const String cstICDepartmentAttributesObjectName = "ICDepartmentAttributes";
        public const String cstICDepartmentAttributeValuesObjectName = "ICDepartmentAttributeValues";

        public const String cstICDepartmentNameColumnName = "ICDepartmentName";
        public const String cstICProductGroupIDColumnName = "ICProductGroupID";
        public const String cstICProductGroupNameColumnName = "ICProductGroupName";
        public const String cstICDepartmentAttributeIDColumnName = "ICDepartmentAttributeID";
        public const String cstICDepartmentAttributeNameColumnName = "ICDepartmentAttributeName";

        public const String cstICDepartmentAttributesComboboxControlName = "fld_cmbAttributes";
        #endregion
        
        #region Variables
        private int PrevSelectedIndex = 0;
        #endregion

        public DepartmentModule()
        {
            Name = "Department";
            CurrentModuleEntity = new DepartmentEntities();
            CurrentModuleEntity.Module = this;
            InitializeModule();
           
            //Init attribute combobox
            DevExpress.XtraEditors.ComboBoxEdit cmb = (DevExpress.XtraEditors.ComboBoxEdit)Controls[DepartmentModule.cstICDepartmentAttributesComboboxControlName];
            cmb.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            cmb.SelectedIndexChanged += new EventHandler(Combobox_SelectedIndexChanged);
            
            //Init combobox data source
            InitComboboxDataSource();
        }

        #region Product group
        public void AddProductGroupToTreeList()
        {
            DepartmentEntities entity = (DepartmentEntities)CurrentModuleEntity;
            ICProductGroupsInfo objProductGroupsInfo = (ICProductGroupsInfo)entity.ModuleObjects[DepartmentModule.cstICProductGroupsObjectName];

            //Generate category number auto
            ICProductGroupsInfo objSelProductGroupsInfo = (ICProductGroupsInfo)entity.ICProductGroupsList.CurrentObject;
            String number = Convert.ToString(objSelProductGroupsInfo.SubList.Count + 1);
            while (number.Length < 2)
                number = '0' + number;
            objProductGroupsInfo.ICProductGroupNo = objSelProductGroupsInfo.ICProductGroupNo + number;

            //Check valid Number_Hieu.nguyen_06/04/2010
            if (String.IsNullOrEmpty(objProductGroupsInfo.ICProductGroupNo))
            {
                MessageBox.Show("Category number can not be empty.", "#Message#", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            THSTreeList lst = entity.ICProductGroupsList;
            THSTreeListObject currObject = lst.CurrentObject;
            foreach (THSTreeListObject childObject in currObject.SubList)
            {
                if ((childObject as ICProductGroupsInfo).ICProductGroupNo == objProductGroupsInfo.ICProductGroupNo)
                {
                    MessageBox.Show("Category number can not be same as another.", "#Message#", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            if (String.IsNullOrEmpty(objProductGroupsInfo.ICProductGroupName))
            {
                MessageBox.Show("Category name can not be empty.", "#Message#", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            objProductGroupsInfo.FK_ICDepartmentID = 0;
            entity.ICProductGroupsList.AddObjectToList();
        }

        public void ChangeProductGroupToTreeList()
        {
            DepartmentEntities entity = (DepartmentEntities)CurrentModuleEntity;
            ICProductGroupsInfo objProductGroupsInfo = (ICProductGroupsInfo)entity.ModuleObjects[DepartmentModule.cstICProductGroupsObjectName];

            //Check valid Number_Hieu.nguyen_06/04/2010
            if (String.IsNullOrEmpty(objProductGroupsInfo.ICProductGroupNo))
            {
                MessageBox.Show("Category number can not be empty.", "#Message#", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (String.IsNullOrEmpty(objProductGroupsInfo.ICProductGroupName))
            {
                MessageBox.Show("Category name can not be empty.", "#Message#", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            ((DepartmentEntities)CurrentModuleEntity).ICProductGroupsList.ChangeObjectFromList();
        }

        public void DeleteProductGroupFromTreeList()
        {
            ((DepartmentEntities)CurrentModuleEntity).ICProductGroupsList.RemoveSelectedRowObjectFromList();
        }
        #endregion

        #region Attribute
        public void ChangeAttribute()
        {
            DevExpress.XtraEditors.ComboBoxEdit cmb = (DevExpress.XtraEditors.ComboBoxEdit)Controls[DepartmentModule.cstICDepartmentAttributesComboboxControlName];
            if (cmb.SelectedIndex > 0)
            {
                DepartmentEntities entity = (DepartmentEntities)CurrentModuleEntity;
                ICDepartmentAttributesInfo objDepartmentAttributesInfo = (ICDepartmentAttributesInfo)CurrentModuleEntity.ModuleObjects[DepartmentModule.cstICDepartmentAttributesObjectName];
                guiAttributeInput guiAttributeInput = new guiAttributeInput(objDepartmentAttributesInfo.ICDepartmentAttributeName, objDepartmentAttributesInfo.ICDepartmentAttributeDesc);
                if (guiAttributeInput.ShowDialog() == DialogResult.OK)
                {
                    //Update combobox item
                    cmb.Properties.Items[cmb.SelectedIndex] = guiAttributeInput.fld_txtAttributeName.Text;
                    cmb.Text = guiAttributeInput.fld_txtAttributeName.Text;
                    //Update module object list
                    int pos = entity.ICDepartmentAttributesList.PosOf(DepartmentModule.cstICDepartmentAttributeNameColumnName, objDepartmentAttributesInfo.ICDepartmentAttributeName);
                    if (pos >= 0)
                    {
                        entity.ICDepartmentAttributesList[pos].ICDepartmentAttributeName = guiAttributeInput.fld_txtAttributeName.Text;
                        entity.ICDepartmentAttributesList[pos].ICDepartmentAttributeDesc = guiAttributeInput.fld_txtAttributeDesc.Text;
                    }
                    //Update module object
                    objDepartmentAttributesInfo.ICDepartmentAttributeName = guiAttributeInput.fld_txtAttributeName.Text;
                    objDepartmentAttributesInfo.ICDepartmentAttributeDesc = guiAttributeInput.fld_txtAttributeDesc.Text;
                }
            }
        }

        public void DeleteAttribute()
        {
            DevExpress.XtraEditors.ComboBoxEdit cmb = (DevExpress.XtraEditors.ComboBoxEdit)Controls[DepartmentModule.cstICDepartmentAttributesComboboxControlName];
            if (cmb.SelectedIndex > 0)
            {
                //Remove item from module object list
                DepartmentEntities entity = (DepartmentEntities)CurrentModuleEntity;
                ICDepartmentAttributesInfo objDepartmentAttributesInfo = (ICDepartmentAttributesInfo)CurrentModuleEntity.ModuleObjects[DepartmentModule.cstICDepartmentAttributesObjectName];
                int pos = entity.ICDepartmentAttributesList.PosOf(DepartmentModule.cstICDepartmentAttributeNameColumnName, objDepartmentAttributesInfo.ICDepartmentAttributeName);
                entity.ICDepartmentAttributesList.RemoveAt(pos);

                //Remove item from combobox
                cmb.Properties.Items.RemoveAt(cmb.SelectedIndex);
                PrevSelectedIndex = 0;
                if (cmb.Properties.Items.Count > 1)
                    cmb.SelectedIndex = 1;
                else
                {
                    cmb.Text = String.Empty;
                    ((DepartmentEntities)CurrentModuleEntity).ICDepartmentAttributeValuesList.Invalidate(new DataSet());
                }
            }
        }

        public void InitComboboxDataSource()
        {
            DepartmentEntities entity = (DepartmentEntities)CurrentModuleEntity;
            DevExpress.XtraEditors.ComboBoxEdit cmb = (DevExpress.XtraEditors.ComboBoxEdit)Controls[DepartmentModule.cstICDepartmentAttributesComboboxControlName];
            cmb.Properties.Items.Clear();
            cmb.Properties.Items.Add("<Add new>");
            foreach (ICDepartmentAttributesInfo objDepartmenAttributesInfo in entity.ICDepartmentAttributesList)
                cmb.Properties.Items.Add(objDepartmenAttributesInfo.ICDepartmentAttributeName);
            PrevSelectedIndex = 0;
            if (cmb.Properties.Items.Count > 1)
            {
                if (cmb.SelectedIndex == 1)
                    Combobox_SelectedIndexChanged(new object(), new EventArgs());
                cmb.SelectedIndex = 1;
            }
            else
                cmb.Text = String.Empty;
        }

        public int GetItemIndex(String itemName)
        {
            DevExpress.XtraEditors.ComboBoxEdit cmb = (DevExpress.XtraEditors.ComboBoxEdit)Controls[DepartmentModule.cstICDepartmentAttributesComboboxControlName];
            return cmb.Properties.Items.IndexOf(itemName);
        }

        protected void Combobox_SelectedIndexChanged(object obj, EventArgs e)
        {
            DepartmentEntities entity = (DepartmentEntities)CurrentModuleEntity;
            DevExpress.XtraEditors.ComboBoxEdit cmb = (DevExpress.XtraEditors.ComboBoxEdit)Controls[DepartmentModule.cstICDepartmentAttributesComboboxControlName];
            if (cmb.SelectedIndex >= 0)
            {
                if (cmb.SelectedIndex == 0)
                {
                    guiAttributeInput guiAttributeInput = new guiAttributeInput();
                    if (guiAttributeInput.ShowDialog() == DialogResult.OK)
                    {
                        entity.ICDepartmentAttributesList.Add(new ICDepartmentAttributesInfo(
                                                                        guiAttributeInput.fld_txtAttributeName.Text,
                                                                        guiAttributeInput.fld_txtAttributeDesc.Text));
                        cmb.Properties.Items.Add(guiAttributeInput.fld_txtAttributeName.Text);
                        cmb.SelectedIndex = cmb.Properties.Items.Count - 1;
                    }
                    else if (PrevSelectedIndex > 0)
                        cmb.SelectedIndex = PrevSelectedIndex;
                    else
                        cmb.Text = String.Empty;
                }
                else
                {
                    int pos;
                    if (PrevSelectedIndex > 0)
                    {
                        pos = entity.ICDepartmentAttributesList.PosOf(DepartmentModule.cstICDepartmentAttributeNameColumnName, cmb.Properties.Items[PrevSelectedIndex].ToString());
                        if (pos >= 0)
                        {
                            ICDepartmentAttributesInfo objDepartmentAttributesInfo = entity.ICDepartmentAttributesList[pos];
                            objDepartmentAttributesInfo.ICDepartmentAttributeValuesList.Clear();
                            foreach (ICDepartmentAttributeValuesInfo objDepartmentAttributeValuesInfo in entity.ICDepartmentAttributeValuesList)
                                objDepartmentAttributesInfo.ICDepartmentAttributeValuesList.Add((ICDepartmentAttributeValuesInfo)objDepartmentAttributeValuesInfo.Clone());
                        }
                    }

                    pos = entity.ICDepartmentAttributesList.PosOf(DepartmentModule.cstICDepartmentAttributeNameColumnName, cmb.Properties.Items[cmb.SelectedIndex].ToString());
                    if (pos >= 0)
                    {
                        entity.ModuleObjects[DepartmentModule.cstICDepartmentAttributesObjectName] = (ICDepartmentAttributesInfo)entity.ICDepartmentAttributesList[pos].Clone();
                        entity.UpdateModuleObjectsBindingSource();
                        entity.ICDepartmentAttributeValuesList.Invalidate(entity.ICDepartmentAttributesList[pos].ICDepartmentAttributeValuesList);
                    }

                }
                PrevSelectedIndex = cmb.SelectedIndex;
            }
        }
        #endregion
    }
    #endregion
}